<?php


namespace app\miniProgram\controller;


use app\miniProgram\model\MaterialKucunView;
use think\facade\Db;

class Other extends Auth
{

    public function getMaterial()
    {
        $name=$this->param['mdName']??'';
        if (!$name){
            return $this->jump(1);
        }
        $where[]=['status','=',1];
        if ($name){
            $where[]=['name','like','%'.$name.'%'];
        }
        $list=Db::connect('company')->name('material_dossier')->where($where)->field('id ,name ,bar_code')->select();
        $data=[];
        foreach ($list as $v){
            $data[]=[
                'label'=>$v['name'].','.$v['bar_code'],
                'value'=>$v['id']
            ];
        }
        return $this->jump(['code'=>1,'message'=>'获取成功','data'=>$data]);
    }

    public function getMaterialInfo()
    {
        $id=$this->param['mdId']??'';
        $bar_code=$this->param['bar_code']??'';
        $where[]=['status','=',1];
        if (!$id&&!$bar_code){
            return $this->jump(0,'参数缺失');
        }
        if ($id){
            $where[]=['id','=',$id];
        }
        if ($bar_code){
            $where[]=['bar_code','=',$bar_code];
        }
        $info=Db::connect('company')->name('material_dossier')->where($where)
            ->field("*,(select name from fy_material_type mt where mt.id=fy_material_dossier.type)typeName,
            (select name from fy_warehouse w where w.id=warehouse_id ) wName")
            ->find();
        if (!$info){
            return $this->jump(['code'=>-2,'message'=>'物料不存在','data'=>$info]);
        }
        return $this->jump(['code'=>1,'message'=>'获取成功','data'=>$info]);
    }
    public function getWarehouse()
    {
        $name=$this->param['name']??'';
        $where[]=['status','=',1];
        if ($name){
            $where[]=['name','like','%'.$name.'%'];
        }
        $list=Db::connect('company')->name('warehouse')->where($where)->field('id as value,name as label')->select();
        return $this->jump(['code'=>1,'message'=>'获取成功','data'=>$list]);
    }

    public function getywtype()
    {
        $type=$this->param['type']??1;
        if ($type==1){
            $list=config('config.inReceiptType');
        }else{
            $list=config('config.outReceiptType');
        }
        $res=[];
        foreach ($list as $v){
            $res[]=['label'=>$v,'value'=>$v];
        }
        return $this->jump(['code'=>1,'message'=>'获取成功','data'=>$res]);
    }

    public function getKucunInfo()
    {
        $wId=$this->param['wId']??'';
        $bar_code=$this->param['bar_code']??'';
        $mdId=$this->param['mdId']??'';
        if (!$wId){
            return $this->jump(0,'参数缺失');
        }
        if (!$mdId&&!$bar_code){
            return $this->jump(0,'参数缺失');
        }

        $where[]=['wId','=',$wId];
        if ($bar_code){
            $where[]=['bar_code','=',$bar_code];
        }
        if ($mdId){
            $where[]=['mdId','=',$mdId];
        }
        $info=MaterialKucunView::actionInfo($where);
        if (!$info){
            return $this->jump(-2,'未入库物料,请使用盘盈入库',$info);
        }
        return $this->jump(1,'获取成功',$info);
    }

    /**
     *
     * @return bool|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @autho:xiangzi
     * @auth:true
     * @Date:2024/8/14
     */
    public function getBgInfo()
    {
        $printCode=$this->param['printCode']??"";
        if (!$printCode){
            return $this->jump(0,'参数缺失');
        }
        $where[]=['ppd.status','=',1];
        $where[]=['ppd.printCode','=',$printCode];
        $info=Db::connect('company')->name('production_plan_detail')->alias('ppd')
            ->join('material_dossier md','md.id=ppd.material_dossier_id','left')
            ->field('ppd.material_dossier_id,ppd.production_plan_id,md.name,md.specs,md.bar_code,md.unit,ppd.craft_route_id')
            ->where($where)
            ->find();
        if (!$info){
            return $this->jump(-2,'数据不存在');
        }
        $where1[]=['cr.status','=',1];
        $where1[]=['crd.status','=',1];
        $where1[]=['crd.craft_route_id','=',$info['craft_route_id']];
        $where1[]=['crd.material_dossier_id','=',$info['material_dossier_id']];
        $list=Db::connect('company')->name('craft_route_detail')->alias('crd')
            ->join('craft_route cr','cr.id=crd.craft_route_id','left')
            ->where($where1)
            ->field("crd.id as value,(select name from fy_craft c where c.id=crd.caraf_id) label")
            ->select();

        return $this->jump(1,'获取成功',['mdInfo'=>$info,'list'=>$list]);
    }
    /**
     * date:2024/4/25
     *工序申报班组/个人id
     */
    public function getPricingMode()
    {
        $param=request()->param();
        $type=$param['type']??'';
        if (!$type){
            return $this->jump(0,'参数缺失') ;
        }
        if ($type=='个人'){
            $list=Db::connect('company')->name('admin')->where('status',1)
                ->field('id as value,name as label')->select();
        }else{
            $list=Db::connect('company')->name('team')->where('status',1)
                ->field('id as value,name as label')->select();
        }
        return $this->jump(1,'获取成功',$list) ;
    }

    /**
     * @return bool|\think\response\Json
     * @throws \think\db\exception\DbException
     * @autho:xiangzi
     * @auth:true
     * @Date:2024/11/7
     * 盘点列表
     */
    public function getPdList()
    {
        $where[]=['deleted','=',0];
        $where[]=['status','=',1];
        $limit=$this->param['limit']??15;
        $list=Db::name('inventory_order')->where($where)->order('id DESC')
            ->field("*,(select name from fy_warehouse w where w.id=fy_inventory_order.warehouse_id)wName,
            (select count(id) from fy_inventory_order_detail  iod 
            where iod.inventory_id=fy_inventory_order.id and iod.status >-1 and iod.deleted =0)countCode,
            (select count(id) from fy_inventory_order_detail  iod 
            where iod.inventory_id=fy_inventory_order.id and iod.isDo=1 and iod.status >-1 and iod.deleted =0)coundDo")
            ->paginate($limit);
        return $this->jump(['code'=>1,'message'=>'操作成功','data'=>$list]);

    }

    /**
     * @return bool|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @autho:xiangzi
     * @auth:true
     * @Date:2024/11/8
     * 盘点明细
     */
    public function getPdMxInfo()
    {
        $where[]=['iod.deleted','=',0];
        $where[]=['iod.status','>',-1];
        $bar_code=$this->param['bar_code']??'';
        $mdId=$this->param['mdId']??'';
        $orderId=$this->param['orderId']??'';
        if (!$orderId){
            return $this->jump(0,'参数缺失');
        }
        $where[]=['iod.inventory_id','=',$orderId];
        if (!$mdId&&!$bar_code){
            return $this->jump(0,'参数缺失');
        }
        if ($mdId){
            $where[]=['iod.material_dossier_id','=',$mdId];
        }
        if ($bar_code){
            $where[]=['md.bar_code','=',$bar_code];
        }
        $info=Db::name('inventory_order_detail')->alias('iod')
            ->where($where)
            ->leftJoin('material_dossier md','md.id=iod.material_dossier_id')
            ->leftJoin('material_type mt','mt.id=md.type')
            ->field("iod.*,md.bar_code,md.name as mdName,mt.name as typeName,md.brand,md.specs,md.unit")
            ->find();
        if (!$info){
            return $this->jump(-2,'未入库物料,请使用盘盈入库',$info);
        }
        return $this->jump(1,'获取成功',$info);

    }
}